package com.oocl.o2o.merchant.jms;

import java.io.IOException;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.o2o.connectutil.dto.AdvertFeedbackToMDTO;
import com.oocl.o2o.merchant.service.AdvertisementManager;

public class AdvertisementListener implements MessageListener {
	@Autowired
	private AdvertisementManager advertisementmanager;

	@Override
	@Transactional
	public void onMessage(Message message) {
		if(message instanceof TextMessage) {
			try {
				String text = ((TextMessage) message).getText();
				System.out.println(text);
				ObjectMapper mapper = new ObjectMapper();
				AdvertFeedbackToMDTO feedback = mapper.readValue(text, AdvertFeedbackToMDTO.class);
				System.out.println(feedback);
				advertisementmanager.receiveFeedBack(feedback);
				message.acknowledge();
			} catch (JMSException | IOException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("Message must be of type TextMessage");
		}

	}

}
